/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2008-2014 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef OSGEARTH_TERRAIN_TILE_MODEL_FACTORY_H
#define OSGEARTH_TERRAIN_TILE_MODEL_FACTORY_H 1

#include <osgEarth/TerrainTileModel>
#include <osgEarth/TerrainOptions>
#include <osgEarth/TerrainEngineRequirements>
#include <osgEarth/ImageLayer>
#include <osgEarth/Progress>
#include <osgEarth/Threading>
#include <osgEarth/ElevationPool>

namespace osgEarth
{
    class ElevationLayerVector;
    class Map;

    /**
     * Set of layers that the TerrainTileModelFactory can use
     * to determine what data to create for a tile. If the manifest
     * is empty, that represents that the factory should create
     * data for ALL available layers. But if it contains at least
     * one entry, the creation is limited to those entries.
     */
    class OSGEARTH_EXPORT CreateTileManifest
    {
    public:
        CreateTileManifest();

        //! Request data for a layer
        void insert(const Layer* layer);

        //! Does the manifest exclude this layer?
        bool excludes(const Layer* layer) const;

        //! Sets whether to apply the update progressively (in LOD order)
        void setProgressive(bool value);
        const optional<bool>& progressive() const { return _progressive; }

        //! Is the manifest empty (meaning ALL available layers should be loaded)?
        bool empty() const;

        //! Are the layers in the manifest up to date with the layers in the map?
        bool inSyncWith(const Map*) const;

        //! Update all the manifest layers with the latest layer revisions from the map
        void updateRevisions(const Map*);

        bool includes(UID uid) const;

        bool includes(const Layer* layer) const;

        bool includesElevation() const;

        bool includesConstraints() const;

        bool includesLandCover() const;

    private:
        typedef vector_map<UID, int> LayerTable;
        LayerTable _layers;
        bool _includesElevation;
        bool _includesConstraints;
        bool _includesLandCover;
        optional<bool> _progressive;
    };

    /**
     * Builds a TerrainTileModel from a map frame.
     */
    class OSGEARTH_EXPORT TerrainTileModelFactory : public osg::Referenced
    {
    public:
        /** Constructor */
        TerrainTileModelFactory(const TerrainOptions& options);

        /**
         * Creates a tile model and populates it with data from the map.
         *
         * @param map          Map frame from which to read source data
         * @param key          Tile key for which to create the model
         * @param manifest     Set of layers for which to fetch data (empty => all layers)
         * @param requirements Hints that tell the factory what types of data to add
         * @param progress     Progress tracking callback
         */
        virtual TerrainTileModel* createTileModel(
            const Map*                       map,
            const TileKey&                   key,
            const CreateTileManifest&        manifest,
            const TerrainEngineRequirements* requirements,
            ProgressCallback*                progress);

        //! Same as createTileModel, except that this method will create "fallback"
        //! data for each layer that doesn't have real data at the TileKey's LOD.
        //! The texture matrix will each layer will provide a scale and bias for
        //! sampling the corresponding texture.
        virtual TerrainTileModel* createStandaloneTileModel(
            const Map*                       map,
            const TileKey&                   key,
            const CreateTileManifest&        manifest,
            const TerrainEngineRequirements* requirements,
            ProgressCallback*                progress);

    protected:

        virtual void addColorLayers(
            TerrainTileModel*                model,
            const Map*                       map,
            const TerrainEngineRequirements* reqs,
            const TileKey&                   key,
            const CreateTileManifest&        manifest,
            ProgressCallback*                progress,
            bool                             standalone);

        virtual TerrainTileImageLayerModel* addImageLayer(
            TerrainTileModel* model,
            ImageLayer* layer,
            const TileKey& key,
            const TerrainEngineRequirements* reqs,
            ProgressCallback* progress);

        virtual void addStandaloneImageLayer(
            TerrainTileModel* model,
            ImageLayer* layer,
            const TileKey& key,
            const TerrainEngineRequirements* reqs,
            ProgressCallback* progress);

        virtual void addElevation(
            TerrainTileModel*            model,
            const Map*                   map,
            const TileKey&               key,
            const CreateTileManifest&    manifest,
            unsigned                     border,
            ProgressCallback*            progress);

        virtual void addStandaloneElevation(
            TerrainTileModel*            model,
            const Map*                   map,
            const TileKey&               key,
            const CreateTileManifest&    manifest,
            unsigned                     border,
            ProgressCallback*            progress);

        virtual TerrainTileLandCoverModel* addLandCover(
            TerrainTileModel*            model,
            const Map*                   map,
            const TileKey&               key,
            const TerrainEngineRequirements* requirements,
            const CreateTileManifest&    manifest,
            ProgressCallback*            progress);

        virtual void addStandaloneLandCover(
            TerrainTileModel*            model,
            const Map*                   map,
            const TileKey&               key,
            const TerrainEngineRequirements* requirements,
            const CreateTileManifest&    manifest,
            ProgressCallback*            progress);

        //virtual void addPatchLayers(
        //    TerrainTileModel*            model,
        //    const Map*                   map,
        //    const TileKey&               key,
        //    const CreateTileManifest&    manifest,
        //    ProgressCallback*            progress,
        //    bool                         fallback);

    protected:

        osg::Texture* createImageTexture(
            const osg::Image* image,
            const ImageLayer* layer) const;

        osg::Texture* createCoverageTexture(
            const osg::Image* image) const;

        osg::Texture* createElevationTexture(
            const osg::Image* image) const;

        TerrainOptions _options;
        osg::ref_ptr<osg::Texture> _emptyColorTexture;
        osg::ref_ptr<osg::Texture> _emptyLandCoverTexture;
        ElevationPool::WorkingSet _workingSet;
    };
}

#endif // OSGEARTH_TERRAIN_TILE_MODEL_FACTORY_H
